<!DOCTYPE html>
<html>
<head>
<title>Throwing in the context of an accessor setter (235223)</title>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
</head>
<body>
<script type="text/javascript">
var frame = document.createElement('iframe');
document.body.appendChild(frame);
var frameWindow = frame.contentWindow;

function assert_dom_exception_in_frame(exception) {
    assert_equals(frameWindow.DOMException.prototype, Object.getPrototypeOf(exception));
    assert_not_equals(DOMException.prototype, Object.getPrototypeOf(exception));
}

function assert_dom_exception_in_incumbent(exception) {
    assert_not_equals(frameWindow.DOMException.prototype, Object.getPrototypeOf(exception));
    assert_equals(DOMException.prototype, Object.getPrototypeOf(exception));
}

test(function () {
    // Sanity check over functions.
    try {
        var element = frameWindow.document.createElement('textarea');
        element.appendChild(element);
        assert_unreached("Cyclic appendChild() should throw HierarchyRequestError.");
    } catch (e) {
        assert_true(e.name == "HierarchyRequestError");
        assert_dom_exception_in_frame(e);
    }
    try {
        var element = frameWindow.document.createElement('textarea');
        Node.prototype.appendChild.call(element, element);
        assert_unreached("Cyclic appendChild() should throw HierarchyRequestError.");
    } catch (e) {
        assert_true(e.name == "HierarchyRequestError");
        assert_dom_exception_in_incumbent(e);
    }
}, "Check that DOM exception is created in called function's context.");

test(function () {
    try {
        var input = frameWindow.document.createElement('input');
        input.size = 0;
        assert_unreached("Setting input.size to zero should throw IndexSizeError.");
    } catch (e) {
        assert_true(e.name == "IndexSizeError");
        assert_dom_exception_in_frame(e);
    }
    try {
        var input = frameWindow.document.createElement('input');
        var pd = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'size');
        pd.set.call(input, 0);
        assert_unreached("Setting input.size to zero should throw IndexSizeError.");
    } catch (e) {
        assert_true(e.name == "IndexSizeError");
        assert_dom_exception_in_incumbent(e);
    }

    try {
        var xhr = new frameWindow.XMLHttpRequest();
        xhr.open('GET', 'nonExistent.html', false);
        xhr.timeout = 10;
        assert_unreached("Setting xhr.timeout on sync XHR object should throw InvalidAccessError.");
    } catch (e) {
        assert_true(e.name == "InvalidAccessError");
        assert_dom_exception_in_frame(e);
    }
    try {
        var xhr = new frameWindow.XMLHttpRequest();
        xhr.open('GET', 'nonExistent.html', false);
        var pd = Object.getOwnPropertyDescriptor(XMLHttpRequest.prototype, 'timeout');
        pd.set.call(xhr, 10);
        assert_unreached("Setting xhr.timeout on sync XHR object should throw InvalidAccessError.");
    } catch (e) {
        assert_true(e.name == "InvalidAccessError");
        assert_dom_exception_in_incumbent(e);
    }
}, "Check that DOM exception is created in setter's context.");

test(function () {
    try {
        var serializer = new frameWindow.XMLSerializer();
        serializer.serializeToString(null);
        assert_unreached("serializing null should throw a TypeError");
    } catch (e) {
        assert_true(e.name == "TypeError");
        assert_equals(frameWindow.TypeError.prototype, Object.getPrototypeOf(e));
        assert_not_equals(TypeError.prototype, Object.getPrototypeOf(e));
    }
    try {
        var serializer = new frameWindow.XMLSerializer();
        XMLSerializer.prototype.serializeToString.call(serializer, null);
        assert_unreached("serializing null should throw a TypeError");
    } catch (e) {
        assert_true(e.name == "TypeError");
        assert_not_equals(frameWindow.TypeError.prototype, Object.getPrototypeOf(e));
        assert_equals(TypeError.prototype, Object.getPrototypeOf(e));
    }
}, "Check that native exception is created in setter's context.");
</script>
</body>
</html>
